﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PersonalWebSite.Common.Google.DTO
{
    /// <summary>
    /// Defines a GeoCode
    /// </summary>
    public class Geocode
    {
        /// <summary>
        /// Tries to parse the given string to a valid Geocode object
        /// </summary>
        /// <remarks>
        /// If parsing the lat or long value fails, only the OriginalFormat will be initialized
        /// </remarks>
        /// <param name="geocodeString">geocodeString</param>
        /// <returns>Geocode</returns>
        public static Geocode TryParse(string geocodeString)
        {
            if(string.IsNullOrWhiteSpace(geocodeString))
                return null;

            string[] geocodeParts = geocodeString.Split(' ', '/', ',');
            if(geocodeParts.Length != 2)
                return null;

            decimal lat = default(decimal);
            decimal lng = default(decimal);
            if (!decimal.TryParse(geocodeParts[0].Trim(), out lat) || !decimal.TryParse(geocodeParts[1].Trim(), out lng))
                return null;

            return new Geocode { OriginalFormat = geocodeString, Latitude = lat, Longitude = lng };
        }

        /// <summary>
        /// Gets or Sets the Latitude
        /// </summary>
        public decimal Latitude { get; set; }

        /// <summary>
        /// Gets or Sets the Latitude
        /// </summary>
        public decimal Longitude { get; set; }

        /// <summary>
        /// Gets or Sets the Original Format of the Geocode
        /// </summary>
        public string OriginalFormat { get; set; }
    }
}